<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/29
 * Time: 10:16
 */

define("HEIMAO_URL", "http://www.t-cat.com.tw/Inquire/Trace.aspx?no=");

function getheimaoStatusByAPI($kd_num)
{
    $no_html = http_get(trim($kd_num));//发送请求

    //因为不是数据接口，所以返回的是个html页面 所以要来抓取页面里面的东西
    $zz_res = format_html($no_html, trim($kd_num));
    return $zz_res[1];
}


function http_get($value)
{
    $url = HEIMAO_URL . $value;
    $ch = curl_init();
    $timeout = 60;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}


function format_html($html, $id)
{
    $result = '';
    if (empty($html)) {
        return $result;
    }


    $sub_str = "<table>";
    $sub_str_td = "您輸入的貨物追蹤查詢號碼如下";

    if (strpos($html, $sub_str)) {
        //位置开始后面的字符串
        $real_html = substr($html, 0, strpos($html, $sub_str));
        if (!empty($real_html) || strpos($real_html, '<tr>') != false) {
            $t1_html = substr($real_html, strpos($real_html, '<tr>'), (strlen($real_html) - strpos($real_html, '<tr>')));
            $t2_html = substr($t1_html, strpos($t1_html, '</tr>'), (strlen($t1_html) - strpos($t1_html, '</tr>')));
            //正则匹配
            $matchall = array();
            preg_match_all('/\<td.+?\>(.+?)\<\/td\>/', $t2_html, $matchall);

        }


        $string = strip_tags($matchall[0][0]);
        $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
        $nm[0] = trim($string);

        $string1 = strip_tags($matchall[0][1]);
        $string1 = htmlspecialchars($string1, ENT_QUOTES, 'UTF-8');
        $nm[1] = trim($string1);

        $string2 = strip_tags($matchall[0][2]);
        $string2 = htmlspecialchars($string2, ENT_QUOTES, 'UTF-8');
        $nm[2] = trim($string2);

        $string3 = strip_tags($matchall[0][3]);
        $string3 = htmlspecialchars($string3, ENT_QUOTES, 'UTF-8');
        $nm[3] = trim($string3);

        $result = $nm;

    } else if (strpos($html, $sub_str_td)) {
        //正在配送的状态
        $result = array($id, "未上线", "", "");
    } else {
        //非法的单号
        $result = array($id, "非法的单号", "", "");
    }

    return $result;
}

function logPayInfo($data)
{
    try {
        global $DATABASE_LIST;
        $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
        $user = $DATABASE_LIST[DB_ID]['username'];
        $password = $DATABASE_LIST[DB_ID]['password'];
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");
        $strKey = "";
        $strValue = "";
        $i = 0;
        $sign = ",";
        foreach ($data as $key => $v) {
            $i++;
            if ($i == count($data)) {
                $sign = "";
            }
            $strKey .= "`$key`" . $sign;
            $strValue .= "'$v'" . $sign;
        }

        $sql = "replace into payment_log($strKey) values ($strValue)";

        Utils::createLog("log payInfo->" . $sql);
        $res = $dbh->exec($sql);
        return $res;

    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

function updatePayInfo($sn, $data)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $str = "";
    $sign = ",";
    $i = 0;
    foreach ($data as $k => $v) {
        $i++;
        if ($i == count($data)) {
            $sign = "";
        }
        $str .= "`" . $k . "`" . "=" . "'$v'" . $sign;
    }
    $sql = "UPDATE `payment_log` SET $str WHERE `OrderID` = '$sn'";
    Utils::createLog("update payInfo->" . $sql);
    $ret = $dbh->exec($sql);
    return $ret;
}

function changeOrderSnStatus($sn)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "UPDATE `jh_order_sn_record` SET `status` = 0 WHERE `order_sn` = '$sn'";
    $ret = $dbh->exec($sql);
    return $ret;
}


function saveEmailData($data)
{

    try {
        global $DATABASE_LIST;
        $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
        $user = $DATABASE_LIST[DB_ID]['username'];
        $password = $DATABASE_LIST[DB_ID]['password'];
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");

        $product_id = $data['products_info'];
        $product_id = "'$product_id'";

        $order_sn = $data['order_sn'];
        $order_sn = "'$order_sn'";

        $to_email = $data['to_email'];
        $to_email = "'$to_email'";

        if (isset($data['createtime'])) {
            $createtime = $data['createtime'];
            $modifytime = time();
            $sql = "replace into yiye_mailinfo(`products_info`,`order_sn`,`to_email`,`createtime`,`modifytime`) values ($product_id,$order_sn,$to_email,$createtime,$modifytime)";
        } else {
            $modifytime = time();
            $sql = "replace into yiye_mailinfo(`products_info`,`order_sn`,`to_email`,`modifytime`) values ($product_id,$order_sn,$to_email,$modifytime)";
        }


        $res = $dbh->exec($sql);
        return $res;

    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}


function getKdInfoBySn($sn)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from jh_logistics where order_sn = '$sn'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getOrdersBySn($sn)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from jh_order_sn_record where order_sn='$sn'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}


function getShopCartBySn($sn)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from jh_shop_cart where order_sn='$sn' and iddel = '-1'";
    $sth = $dbh->query($sql);
    $rows = $sth->fetchAll();
    return $rows;
}


function getProductById($pid)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from products where products_id='$pid'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getPayConfig($site)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from pay_configuration where app_site='$site' limit 1";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function lastedInsertId()
{
    $time = time();
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "insert into auto_increment_sn (`createtime`) values ($time)";
    $dbh->exec($sql);
    $id = $dbh->lastInsertId();
    return $id;
}


function home_tongji($id)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select * from tong_ji where id = $id";
    $sth = $dbh->query($sql);
    $row = $sth->fetch(PDO::FETCH_ASSOC);
    return $row;
}

function getOrdersProds($cookieid)
{
    global $DATABASE_LIST;
    $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
    $user = $DATABASE_LIST[DB_ID]['username'];
    $password = $DATABASE_LIST[DB_ID]['password'];
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    $sql = "select o.id as oid,o.product_id,o.product_nums,p.* from jh_shop_cart o,products p where o.product_id = p.products_id and o.userid = '$cookieid' and o.iddel = 1";
    $sth = $dbh->query($sql);
    $results = $sth->fetchAll(PDO::FETCH_ASSOC);
    return $results;
}





function getCity($lan)
{
    if ($lan == 'ZH') {

    } elseif ($lan == 'US') {

    } elseif ($lan == 'HK') {
        $city = array("香港島", "九龍半島", "新界");

        $area = array(
            array('中西區', '東區', '南區', '灣仔區'),
            array('九龍區', '观塘区', '深水埗区', '黃大仙區', '油尖旺區'),
            array('離島區', '葵青區', '北區', '西貢區', '沙田區', '大浦區', '荃灣區', '屯門區', '元朗區')
        );


        foreach ($area[0] as $a) {
            $new_arr0[] = array("n" => $a);
        }
        $city_arr[0] = array("p" => "香港島", "c" => $new_arr0);
        foreach ($area[1] as $a) {
            $new_arr1[] = array("n" => $a);
        }
        $city_arr[1] = array("p" => "九龍半島", "c" => $new_arr1);

        foreach ($area[2] as $a) {
            $new_arr2[] = array("n" => $a);
        }
        $city_arr[2] = array("p" => "新界", "c" => $new_arr2);


    } else {
        $city = array("台北市", "高雄市", "基隆市", "台中市", "台南市", "新竹市", "嘉義市", "新北市", "宜蘭縣", "桃園縣", "新竹縣", "苗粟縣", "彰化縣", "南投縣", "雲林縣", "嘉義縣", "屏東縣", "台東縣", "花蓮縣", "澎湖縣");

        $area = array(
            array('松山區', '大同區', '士林區', '中正區', '內湖區', '中山區', '南港區', '大安區', '文山區', '信義區', '萬華區', '北投區'),
            array('盐埕区', '鼓山区', '左营区', '楠梓区', '三民区', '新兴区', '新兴区', '苓雅区', '前镇区', '旗津区', '小港区'),
            array('中正区', '七堵区', '暖暖区', '仁爱区', '中山区', '安乐区', '信义区'),
            array('東區', '西區', '南區', '北區', '中區', '西屯區', '南屯區', '北屯區'),
            array('東區', '南區', '北區', '安平區', '安南區'),
            array('東區', '北區', '香山区'),
            array('東區', '西區'),
            array('板桥市', '三重市', '中和市', '永和市', '新庄市', '新店市', '树林市', '汐止市', '土城市', '芦洲市', '莺歌镇', '三峡镇', '淡水镇', '瑞芳镇', '五股乡', '泰山乡', '林口乡', '深坑乡', '石碇乡', '坪林乡', '三芝乡', '石门乡', '八里乡', '平溪乡', '双溪乡', '贡寮乡', '金山乡', '万里乡', '乌来乡'),
            array('宜兰市', '罗东镇', '苏澳镇', '头城镇', '礁溪乡', '壮围乡', '员山乡', '冬山乡', '五结乡', '三星乡', '大同乡', '南澳乡'),
            array('桃园市', '中坜市', '八德市', '平镇市', '大溪镇', '杨梅镇', '芦竹乡', '大园乡', '龟山乡', '龙潭乡', '新屋乡', '观音乡', '复兴乡'),
            array('竹北市', '竹东镇', '新埔镇', '关西镇', '湖口乡', '新丰乡', '芎林乡', '横山乡', '北埔乡', '宝山乡', '峨眉乡', '尖石乡', '五峰乡'),
            array('苗栗市', '苑里镇', '通霄镇', '竹南镇', '头份镇', '后龙镇', '卓兰镇', '大湖乡', '公馆乡', '铜锣乡', '南庄乡', '头屋乡', '三义乡', '西湖乡', '造桥乡', '三湾乡', '狮潭乡', '泰安乡'),
            array('彰化市', '鹿港镇', '和美镇', '员林镇', '溪湖镇', '田中镇', '北斗镇', '二林镇', '线西乡', '伸港乡', '福兴乡', '秀水乡', '花坛乡', '芬园乡', '大村乡', '埔盐乡', '埔心乡', '永靖乡', '社头乡', '二水乡', '田尾乡', '埤头乡', '芳苑乡', '大城乡', '竹塘乡', '溪州乡'),
            array('南投市', '埔里镇', '草屯镇', '竹山镇', '集集镇', '名间乡', '鹿谷乡', '中寮乡', '鱼池乡', '国姓乡', '水里乡', '信义乡', '仁爱乡'),
            array('斗六市', '斗南镇', '虎尾镇', '西螺镇', '土库镇', '北港镇', '古坑乡', '大埤乡', '莿桐乡', '林内乡', '二仑乡', '仑背乡', '麦寮乡', '东势乡', '褒忠乡', '台西乡', '元长乡', '四湖乡', '口湖乡', '水林乡'),
            array('太保市', '朴子市', '布袋镇', '大林镇', '民雄乡', '溪口乡', '新港乡', '六脚乡', '东石乡', '义竹乡', '鹿草乡', '水上乡', '中埔乡', '竹崎乡', '梅山乡', '番路乡', '大埔乡', '阿里山乡'),
            array('屏东市', '潮州镇', '东港镇', '恒春镇', '万丹乡', '长治乡', '麟洛乡', '九如乡', '里港乡', '盐埔乡', '高树乡', '万峦乡', '内埔乡', '竹田乡', '新埤乡', '枋寮乡', '新园乡', '崁顶乡', '林边乡', '南州乡', '佳冬乡', '琉球乡', '车城乡', '满州乡', '枋山乡', '三地门乡', '雾台乡', '玛家乡', '泰武乡', '来义乡', '春日乡', '狮子乡', '牡丹乡'),
            array('台东市', '成功镇', '关山镇', '卑南乡', '鹿野乡', '池上乡', '东河乡', '长滨乡', '太麻里乡', '大武乡', '绿岛乡', '海端乡', '延平乡', '金峰乡', '达仁乡', '兰屿乡'),
            array('花莲市', '凤林镇', '玉里镇', '新城乡', '吉安乡', '寿丰乡', '光复乡', '丰滨乡', '瑞穗乡', '富里乡', '秀林乡', '万荣乡', '卓溪乡'),
            array('马公市', '湖西乡', '白沙乡', '西屿乡', '望安乡', '七美乡'),
        );


        foreach ($area[0] as $a) {
            $new_arr0[] = array("n" => $a);
        }
        $city_arr[0] = array("p" => "台北市", "c" => $new_arr0);

        foreach ($area[1] as $a) {
            $new_arr1[] = array("n" => $a);
        }
        $city_arr[1] = array("p" => "高雄市", "c" => $new_arr1);

        foreach ($area[2] as $a) {
            $new_arr2[] = array("n" => $a);
        }
        $city_arr[2] = array("p" => "基隆市", "c" => $new_arr2);

        foreach ($area[3] as $a) {
            $new_arr3[] = array("n" => $a);
        }
        $city_arr[3] = array("p" => "台中市", "c" => $new_arr3);


        foreach ($area[4] as $a) {
            $new_arr4[] = array("n" => $a);
        }
        $city_arr[4] = array("p" => "台南市", "c" => $new_arr4);

        foreach ($area[5] as $a) {
            $new_arr5[] = array("n" => $a);
        }
        $city_arr[5] = array("p" => "新竹市", "c" => $new_arr5);

        foreach ($area[6] as $a) {
            $new_arr6[] = array("n" => $a);
        }
        $city_arr[6] = array("p" => "嘉義市", "c" => $new_arr6);

        foreach ($area[7] as $a) {
            $new_arr7[] = array("n" => $a);
        }
        $city_arr[7] = array("p" => "新北市", "c" => $new_arr7);

        foreach ($area[8] as $a) {
            $new_arr8[] = array("n" => $a);
        }
        $city_arr[8] = array("p" => "宜蘭縣", "c" => $new_arr8);

        foreach ($area[9] as $a) {
            $new_arr9[] = array("n" => $a);
        }
        $city_arr[9] = array("p" => "桃園縣", "c" => $new_arr9);

        foreach ($area[10] as $a) {
            $new_arr10[] = array("n" => $a);
        }
        $city_arr[10] = array("p" => "新竹縣", "c" => $new_arr10);

        foreach ($area[11] as $a) {
            $new_arr11[] = array("n" => $a);
        }
        $city_arr[11] = array("p" => "苗粟縣", "c" => $new_arr11);

        foreach ($area[12] as $a) {
            $new_arr12[] = array("n" => $a);
        }
        $city_arr[12] = array("p" => "彰化縣", "c" => $new_arr12);

        foreach ($area[13] as $a) {
            $new_arr13[] = array("n" => $a);
        }
        $city_arr[13] = array("p" => "南投縣", "c" => $new_arr13);

        foreach ($area[14] as $a) {
            $new_arr14[] = array("n" => $a);
        }
        $city_arr[14] = array("p" => "雲林縣", "c" => $new_arr14);

        foreach ($area[15] as $a) {
            $new_arr15[] = array("n" => $a);
        }
        $city_arr[15] = array("p" => "嘉義縣", "c" => $new_arr15);

        foreach ($area[16] as $a) {
            $new_arr16[] = array("n" => $a);
        }
        $city_arr[16] = array("p" => "屏東縣", "c" => $new_arr16);

        foreach ($area[17] as $a) {
            $new_arr17[] = array("n" => $a);
        }
        $city_arr[17] = array("p" => "台東縣", "c" => $new_arr17);


        foreach ($area[18] as $a) {
            $new_arr18[] = array("n" => $a);
        }
        $city_arr[18] = array("p" => "花蓮縣", "c" => $new_arr18);


        foreach ($area[19] as $a) {
            $new_arr19[] = array("n" => $a);
        }
        $city_arr[19] = array("p" => "澎湖縣", "c" => $new_arr19);

    }
    return json_encode(array("citylist" => $city_arr));
}
